package com.frogs.ucenter.v1.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.system.web.api.response.FailResponse;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.AppBaseException;
import com.frogs.ucenter.v1.request.InsuranceCodeAddRequest;
import com.frogs.ucenter.v1.request.InsuranceInfoListRequest;
import com.frogs.ucenter.v1.request.InsuranceRecordAddRequest;
import com.frogs.ucenter.v1.request.InsuranceRecordDeleteRequest;
import com.frogs.ucenter.v1.request.InsuranceRecordDetailRequest;
import com.frogs.ucenter.v1.request.InsuranceRecordListRequest;
import com.frogs.ucenter.v1.service.InsuranceService;
import com.frogs.ucenter.v1.util.ApiConstants;

/**
 * 
 * @author wushubin
 * 保险接口
 */
@Controller(ApiConstants.VERSION+"insuranceController")
@RequestMapping("/"+ApiConstants.VERSION)
public class InsuranceController extends BaseController3 {

	private static final String ROOT = "/insurance";
	private static final String INSURANCE_CODE_ADD = ROOT + "/code" + ADD;
	private static final String INSURANCE_RECORD_LIST = ROOT + "/record" + LIST;
	private static final String INSURANCE_RECORD_ADD = ROOT + "/record" + ADD;
	private static final String INSURANCE_LIST = ROOT + LIST;
	private static final String INSURANCE_RECORD_DETAIL = ROOT + "/record"+ DETAIL;
	private static final String INSURANCE_RECORD_DELETE = ROOT +"/record"+ DELETE;
	
	@Resource(name=ApiConstants.VERSION + "insuranceService")
	private InsuranceService insuranceService;
	
	/**
	 * 生成激活码
	 * @param request
	 * @return
	 */
	@RequestMapping(INSURANCE_CODE_ADD)
	public Response insuranceCodeAdd(HttpServletRequest request) {
		Response response;
		try {
			InsuranceCodeAddRequest req = new InsuranceCodeAddRequest();
			req.parse(request);
			response = insuranceService.insertCode(req);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}

	/**
	 * 创建保单
	 * @param request
	 * @return
	 */
	@RequestMapping(INSURANCE_RECORD_ADD)
	public Response insuranceRecordAdd(HttpServletRequest request) {
		Response response;
		try {
			InsuranceRecordAddRequest req = new InsuranceRecordAddRequest();
			req.parse(request);
			response = insuranceService.insertInsuranceRecord(req);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}

	/**
	 * 保险产品信息列表
	 * @param request
	 * @return
	 */
	@RequestMapping(INSURANCE_LIST)
	public Response insuranceList(HttpServletRequest request) {
		Response response;
		try {
			InsuranceInfoListRequest req = new InsuranceInfoListRequest();
			req.parse(request);
			response = insuranceService.getInsuranceInfoList(req);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	/**
	 * 保单信息列表
	 * @param request
	 * @return
	 */
	@RequestMapping(INSURANCE_RECORD_LIST)
	public Response insuranceRecordList(HttpServletRequest request) {
		Response response;
		try {
			InsuranceRecordListRequest req = new InsuranceRecordListRequest();
			req.parse(request);
			response = insuranceService.getInsuranceRecordList(req);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	/**
	 * 保单详情
	 * @param request
	 * @return
	 */
	@RequestMapping(INSURANCE_RECORD_DETAIL)
	public Response insuranceRecordDetail(HttpServletRequest request) {
		Response response;
		try {
			InsuranceRecordDetailRequest req = new InsuranceRecordDetailRequest();
			req.parse(request);
			response = insuranceService.getInsuranceRecordDetail(req);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	/**
	 * 退保
	 * @param request
	 * @return
	 */
	@RequestMapping(INSURANCE_RECORD_DELETE)
	public Response insuranceRecordDelete(HttpServletRequest request) {
		Response response;
		try {
			InsuranceRecordDeleteRequest req = new InsuranceRecordDeleteRequest();
			req.parse(request);
			response = insuranceService.deleteInsuranceRecord(req);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
}
